from datetime import datetime

import redis
from flask_migrate import Migrate
from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()


def get_redis_connection(rdb=1):
    redis_client = redis.Redis(host='', port='6379', password='', db=rdb, decode_responses=True)
    return redis_client


# test

class Article(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(200), nullable=True)
    body = db.Column(db.Text(99999), nullable=True)
    source = db.Column(db.String(1000), nullable=True)
    source_href = db.Column(db.String(1000), nullable=True)
    headpic = db.Column(db.String(1000), nullable=True)
    abstract = db.Column(db.Text(99999), nullable=True)


synonyms_association_table = db.Table('synonyms',
                                      db.Column('word_id', db.Integer, db.ForeignKey('word.id'), primary_key=True),
                                      db.Column('synonym_id', db.Integer, db.ForeignKey('word.id'), primary_key=True)
                                      )


class Word(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    word = db.Column(db.String(80), unique=True, nullable=False)
    language = db.Column(db.String(20), default='MY')
    sentences = db.relationship('Sentence', backref='word', lazy=True)
    root_word = db.Column(db.String(80))
    root_meaning = db.Column(db.String(80), nullable=True)
    meanings = db.relationship('Meaning', backref='word', lazy=True)
    collocations = db.relationship('Collocation', backref='word', lazy=True)
    # 形近词
    relatives = db.relationship('Relative', backref='word', lazy=True)
    # 长得像的词
    looklikes = db.relationship('Looklike', backref='looklike_word', lazy=True)
    # 同义词
    synonyms = db.relationship(
        'Word',
        secondary=synonyms_association_table,
        primaryjoin=(synonyms_association_table.c.word_id == id),
        secondaryjoin=(synonyms_association_table.c.synonym_id == id),
        backref=db.backref('synonym_to', lazy='dynamic'),
        lazy='dynamic'
    )

    study_logs = db.relationship('StudyLog', backref='word', lazy=True)


class Collocation(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    content = db.Column(db.String(200), nullable=False)
    word_id = db.Column(db.Integer, db.ForeignKey('word.id'), nullable=False)


class Relative(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    content = db.Column(db.String(200), nullable=False)
    word_id = db.Column(db.Integer, db.ForeignKey('word.id'), nullable=False)


class Looklike(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    word = db.Column(db.String(200), nullable=False)
    word_id = db.Column(db.Integer, db.ForeignKey('word.id'), nullable=False)


class Sentence(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    content = db.Column(db.String(200), nullable=False)
    word_id = db.Column(db.Integer, db.ForeignKey('word.id'), nullable=False)


class Meaning(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    part_of_speech = db.Column(db.String(20), nullable=False)
    definition = db.Column(db.String(200), nullable=False)
    word_id = db.Column(db.Integer, db.ForeignKey('word.id'), nullable=False)


class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True, nullable=False)
    phone = db.Column(db.String(20), unique=True, nullable=False)
    avatar = db.Column(db.String(200),
                       default='https://wechat-1317403776.cos.ap-beijing.myqcloud.com/%E5%9B%BE%E7%89%87/R-C.jpg')
    schedule = db.Column(db.Integer, default=20)
    study_logs = db.relationship('StudyLog', backref='user', lazy=True)


class StudyLog(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
    word_id = db.Column(db.Integer, db.ForeignKey('word.id'), nullable=False)
    start_study_time = db.Column(db.DateTime, default=datetime.utcnow)
    study_times = db.Column(db.Integer, default=0)
    next_study_time = db.Column(db.DateTime, nullable=True)
